package com.cherish.screen.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Classname ImageProxyController
 * @Date 2020/4/12 18:03
 * @Created by admin
 * @Description TODO
 */
@Slf4j
@RestController
@RequestMapping("/proxy")
public class ProxyController {

    @GetMapping(value = "/canvas")
    public void canvas(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String url = request.getParameter("url");

        System.out.println("canvas img url" + url);

        String callback = request.getParameter("callback");
        if (url != "" && callback != "") {
            URL urlInfo = new URL(url);
            if (urlInfo.getProtocol().equals("http") || urlInfo.getProtocol().equals("https")) {
                HttpURLConnection conn = (HttpURLConnection) urlInfo.openConnection();
                String contentType = conn.getContentType();
                if (contentType.equals("image/png") || contentType.equals("image/jpg") || contentType.equals("image/jpeg") || contentType.equals("image/gif") || contentType.equals("text/html") || contentType.equals("application/xhtml")) {
                    if (request.getParameter("xhr2") != null) {
                        response.setHeader("Access-Control-Allow-Origin", "*");
                        response.setContentType(contentType);
                        DataInputStream input = new DataInputStream(conn.getInputStream());
                        BufferedOutputStream bout = new BufferedOutputStream(response.getOutputStream());
                        try {
                            byte b[] = new byte[1024];
                            int len = input.read(b);
                            while (len > 0) {
                                bout.write(b, 0, len);
                                len = input.read(b);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            bout.close();
                            input.close();
                        }
                    } else {
                        response.setContentType("application/javascript");
                        if (contentType.equals("text/html") || contentType.equals("application/xhtml")) {
                        } else {
                            // 获取数据流生成byte字节
                            DataInputStream input = new DataInputStream(conn.getInputStream());
                            input.toString();
                            byte[] buffer = new byte[1024 * 8];

                            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
                            byte[] b = new byte[1024 * 8];
                            int n;
                            while ((n = input.read(b)) != -1) {
                                bos.write(b, 0, n);
                            }
                            input.close();
                            bos.close();
                            buffer = bos.toByteArray();
                            // 将byte转成base64
                            BASE64Encoder encode = new BASE64Encoder();
                            String base64data = encode.encode(buffer);
                            base64data = base64data.replaceAll("\r\n", "");
                            response.getWriter().write(callback + "('" + "data:" + contentType + ";base64," + base64data + "')");
                        }
                    }
                }
            }
        }

    }
}
